def countries = [:]
def languages = [:]
def statuses = [:]
def languageNames = [:]

new File("../ethnologue-src/LanguageCodes.tab").eachLine{line->
	line = line.replaceAll(/;/, " /") //replace semi-colons with slashes (To allow lists of locales to use ; as a separator.
	line = line.replaceAll(/"/, "''") //replace double quotes with two single quotes (To avoid messing up the JSON structure)
	line = line.replaceAll(/_/, "-") //replace underscore with dash (To avoid messing up storing the language as a Java locale which uses _ as a language/country/variant separator
	def (language, country, status, name) = line.split(/\t/).toList()

	if(language != "LangID")
	{
		statuses[language] = status
		languageNames[language] = [name:name, country:country, status:status]
	}
}


new File("../ethnologue-src/LanguageIndex.tab").eachLine{line->
	line = line.replaceAll(/;/, " /") //replace semi-colons with slashes (To allow lists of locales to use ; as a separator.
	line = line.replaceAll(/"/, "''") //replace double quotes with two single quotes (To avoid messing up the JSON structure)
	line = line.replaceAll(/_/, "-") //replace underscore with dash (To avoid messing up storing the language as a Java locale which uses _ as a language/country/variant separator
	def (language, country, nameType, name) = line.split(/\t/).toList()

	if(language != "LangID")
	{
		if(countries[country])
		{
			countries[country] << [language:language, name:name, nameType:nameType]
		}
		else
		{
			countries[country] = [[language:language, name:name, nameType:nameType]]
		}
	}
}



new File("../java-src/org/ethnologue/iso639_3/ISO639_3LanguageCodes.java").withWriter("UTF-8"){groovyWriter->

groovyWriter << '''import java.util.HashMap;

public class ISO639_3LanguageCodes
{
	public static ISO639_3LanguageData get(String code)
	{
		if(code == null){return null;}
		code = code.trim();
		if(code.equals("")){return null;}
		code = code.toLowerCase();
		if(! (code.matches("^[a-z]{3,3}"))){return null;}
		String letter = "" + code.charAt(0);
		HashMap codeMap = ((Letter)classMap.get(letter)).codeMap;
		return (ISO639_3LanguageData)(codeMap.get(code));
	}
	
	static class Letter
	{
		static HashMap codeMap = new HashMap();
	}
	
'''

for(letter in "a".."z")
{
	def LETTER = letter.toUpperCase()

	groovyWriter << "\tstatic class ${LETTER} extends Letter\r\n\t{\r\n\t\tstatic\r\n\t\t{\r\n"

	languageNames.keySet().sort().findAll{it.startsWith(letter)}.each{langCode->
		groovyWriter << """\t\t\tcodeMap.put("${langCode}",new ISO639_3LanguageData("${langCode}","${languageNames["$langCode"].name}","${languageNames["$langCode"].country}","${languageNames["$langCode"].status}"));\r\n"""
	}
	
	groovyWriter << "\t\t}\r\n\t}\r\n\r\n"
}

groovyWriter << "\tpublic static HashMap classMap = new HashMap();\r\n\tstatic\r\n\t{\r\n"
for(letter in "a".."z")
{
	groovyWriter << """\t\tclassMap.put("$letter",new ${letter.toUpperCase()}());\r\n"""
}

groovyWriter << "\t}\r\n}\r\n"

}

